package cn.darkhorse.ticket.store.service.verification.impl;


import cn.darkhorse.exception.ServiceException;
import cn.darkhorse.ticket.store.dto.TicketProductDetailsDTO;
import cn.darkhorse.ticket.store.pojo.TicketInventory;
import cn.darkhorse.ticket.store.pojo.TicketInventorySurplus;
import cn.darkhorse.ticket.store.service.TicketInventoryService;
import cn.darkhorse.ticket.store.service.TicketInventorySurplusService;
import cn.darkhorse.ticket.store.service.verification.ICalculationService.IQuantityUsageService;
import cn.darkhorse.ticket.store.service.verification.ICalculationService.MaterialUsage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @description:  校验分支
 * @author: mr.wang
 * @time: 2022/6/6 15:05
 */
public abstract class BaseRules implements IQuantityUsageService {
	
	@Resource
	public TicketInventorySurplusService surplusService;
	@Resource
	public TicketInventoryService inventoryService;
	
	
	public ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
	
	
	@Override
	public void check() {
		this.doCheck();
	}
	
	/**
	 * 分支校验过程
	 * @return
	 */
	protected abstract void doCheck();
	
	

	
	
	
	/**
	 * 使用记录最新一条
	 * @return
	 */
	protected Map<String, TicketInventorySurplus> getSurplusMap() {
		rwl.readLock().lock();
		Map<String, TicketInventorySurplus> surplusMap =  new HashMap<>();;
		try {
			String inSql = String.format("SELECT MAX(tis.id) as id FROM ticket_inventory_surplus tis  " +
					"GROUP BY tis.product_id,tis.warehouse_id DESC");
			QueryWrapper<TicketInventorySurplus> queryWrapper = new QueryWrapper<>();
			queryWrapper.inSql("id", inSql);
			List<TicketInventorySurplus> surplusList = surplusService.list(queryWrapper);
			surplusList.forEach(item -> {
				surplusMap.put(item.getWarehouseId()+"-"+item.getProductId(), item);
			});
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}finally {
			rwl.readLock().unlock();
		}
		return surplusMap;
	}
	
	/**
	 * 库存信息
	 * @return
	 */
	
	protected Map<String, TicketInventory> getInventoryMap() {
		Map<String, TicketInventory> inventoryMap = new HashMap<>();
		rwl.readLock().lock();
		try {
			List<TicketInventory> inventoryList = inventoryService.list();
			inventoryList.forEach(item -> {
				inventoryMap.put(item.getWarehouseInfoId()+"-"+item.getProductId(), item);
			});
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}finally {
			rwl.readLock().unlock();
		}
		return inventoryMap;
	}
	
	/**
	 * 拼接数量校验对象
	 * @param details
	 * @return
	 */
	protected Map<String,TicketProductDetailsDTO> surplusCheckDTOMap(List<TicketProductDetailsDTO> details){
		Map<String,TicketProductDetailsDTO> surplusMap = new HashMap<>();
		details.forEach(item->{
			surplusMap.put(item.getWarehouseInfoId()+"-"+item.getProductId(),item);
		});
		return surplusMap;
	}
	
}
